package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.conmon.enums.SystemCode;
import com.woniuxy.mapper.GoodsMapper;
import com.woniuxy.pojo.Goods;
import com.woniuxy.pojo.dto.GoodsSelectDTO;
import com.woniuxy.pojo.dto.InventoryDTO;
import com.woniuxy.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author 29331
* @description 针对表【t_goods(商品信息表)】的数据库操作Service实现
* @createDate 2025-06-03 10:37:42
*/
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
    implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override

    public Page<Goods> getGoodsByType(Integer typeId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        if (typeId == 0){
            wrapper.orderByAsc(Goods::getSequence);
        }else {
            wrapper.eq(Goods::getTypeId,typeId);
        }
        wrapper.select(Goods::getId,Goods::getName,Goods::getCategory,
                Goods::getImage,Goods::getTitle,Goods::getGoodsNum,
                Goods::getPrice,Goods::getInventory,Goods::getInventoryWarn,Goods::getTypeId);

        Page<Goods> page = new Page<>(pageNum,pageSize,true);
        goodsMapper.selectPage(page,wrapper);
        return page;
    }



    @Override
    public void updateInventory(Long id, InventoryDTO inventoryDTO) {
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Goods::getId,id);
        Goods goods = goodsMapper.selectOne(wrapper);
        goods.setInventory(inventoryDTO.getInventory());
        goods.setInventoryWarn(inventoryDTO.getInventoryWarn());
        goodsMapper.updateById(goods);

    }



    @Override
    public String removeCategoryByIds(List<Long> ids) {

        for (Long id : ids){
            LambdaUpdateWrapper<Goods> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.in(Goods::getId,id);
            Goods goods = goodsMapper.selectOne(updateWrapper);
            if (goods.getTypeId() != 1){
                return SystemCode.REMOVEGOODS_FAILURE.getMessage();
            }

        }
        LambdaUpdateWrapper<Goods> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Goods::getId,ids)
                    .set(Goods::getTypeId,2);
        goodsMapper.update(null,updateWrapper);

       return SystemCode.REMOVEGOODS_SUCCESS.getMessage();
    }

    @Override
    public void addGoods(Goods goods) {

        goodsMapper.insert(goods);

    }

    @Override
    public String upGoodsByIds(List<Long> ids) {

        for (Long id : ids){
            LambdaUpdateWrapper<Goods> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.in(Goods::getId,id);
            Goods goods = goodsMapper.selectOne(updateWrapper);
            if (goods.getTypeId()==4){
                return SystemCode.UPGOODS_FAILURE.getMessage();
            }
        }
        LambdaUpdateWrapper<Goods> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Goods::getId,ids)
                .set(Goods::getTypeId,1);
        goodsMapper.update(null,updateWrapper);
        return SystemCode.UPGOODS_SUCCESS.getMessage();
    }

    @Override
    public Goods selectGoodsById(Long id) {

        return  goodsMapper.selectById(id);
    }

    @Override
    public void updateGoodsById(Goods goods, Long id) {
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Goods::getId,id);
        goodsMapper.update(goods,wrapper);
    }


    @Override
    public void deleteGoodsByIds(List<Long> ids) {
        goodsMapper.deleteBatchIds(ids);
    }

    @Override
    public List<Goods> selectGoods(GoodsSelectDTO goodsSelectDTO) {
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();

        // 商品名称模糊匹配
        if (StringUtils.hasText(goodsSelectDTO.getName())) {
            wrapper.like(Goods::getName, goodsSelectDTO.getName());
        }

        // 商品编号模糊匹配
        if (StringUtils.hasText(goodsSelectDTO.getGoodsNum())) {
            wrapper.like(Goods::getGoodsNum, goodsSelectDTO.getGoodsNum());
        }

        if (StringUtils.hasText(goodsSelectDTO.getCategory())){
            wrapper.eq(Goods::getCategory,goodsSelectDTO.getCategory());
        }

        // 状态筛选
        if (goodsSelectDTO.getTypeId() != null && goodsSelectDTO.getTypeId() > 0) {
            wrapper.eq(Goods::getTypeId, goodsSelectDTO.getTypeId());
        }

        // 价格区间
        if (goodsSelectDTO.getMinPrice() != null) {
            wrapper.ge(Goods::getPrice, goodsSelectDTO.getMinPrice());
        }
        if (goodsSelectDTO.getMaxPrice() != null) {
            wrapper.le(Goods::getPrice, goodsSelectDTO.getMaxPrice());
        }

        // 库存区间
        if (goodsSelectDTO.getMinStock() != null) {
            wrapper.ge(Goods::getInventory, goodsSelectDTO.getMinStock());
        }
        if (goodsSelectDTO.getMaxStock() != null) {
            wrapper.le(Goods::getInventory, goodsSelectDTO.getMaxStock());
        }

        // 创建时间区间
        if (goodsSelectDTO.getStartDate() != null) {
            wrapper.ge(Goods::getCreateTime, goodsSelectDTO.getStartDate());
        }
        if (goodsSelectDTO.getEndDate() != null) {
            wrapper.le(Goods::getCreateTime, goodsSelectDTO.getEndDate());
        }

        // 排序
        wrapper.orderByAsc(Goods::getSequence);

        return goodsMapper.selectList(wrapper);
    }
}




